<template>
  <div class='container'>
      <el-card>
          <el-form  label-width="80px" >
              <el-row>
                <el-col :span="6">
                  <el-form-item label="关键字" prop="info" >
                    <el-input v-model="tableForm.keyword"  style="width:200px"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6" :offset="12">
                  <el-form-item style="text-align:right" >
                    <el-button @click="clear()">清除</el-button>
                    <el-button type="primary" @click="filter()">搜索</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            <el-alert
              :title="`数据一共 ${total} 条`"
              type="info"
              show-icon>
            </el-alert>
          </el-form>
      <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="编号"
            width="250">
          </el-table-column>
          <el-table-column
            label="题型"
            width="100">
            <template #default='scope'>
            {{questionType.find(item=>item.value===+scope.row.questionType).label}}
            </template>
          </el-table-column>
          <el-table-column
            prop="questionIDs"
            label="题目编号"
            width="300">
            <template #default='number'>
              <div v-for="item in number.row.questionIDs" :key="item.number">
                <a  @click='openWindow(item)'>{{item.number}}</a><br>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
            width="250">
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
            width="200">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率%"
            width="200">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人"
            width="200">
          </el-table-column>
          <el-table-column
            label="操作"
            width="180">
            <template #default='{row}'>
                <el-button
                  @click="del(row)"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  plain
                  >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="tableForm.page"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="tableForm.pagesize"
      :total='total'
      layout=" sizes, prev, pager, next, jumper"
      style="text-align:right">
    </el-pagination>
  </div>
      </el-card>
      <QuestionsRpreview ref="questionWindow" :data='questionInfo'></QuestionsRpreview>

  </div>
</template>

<script>
import { randoms, removeRandoms } from '../../api/hmmm/questions'
import { questionType } from '../../api/hmmm/constants'
import QuestionsRpreview from '../components/questions-rpreview.vue'
export default {
  components: {
    QuestionsRpreview
  },
  data () {
    return {
      // 表格请求表单
      tableForm: {
        page: 1,
        pagesize: 4,
        keyword: null
      },
      // 表格数据
      tableData: [],
      // 总数
      total: 0,
      questionType,
      questionInfo: {}
    }
  },
  methods: {
    // 筛选按钮
    filter () {
      this.tableForm.page = 1
      this.getList()
    },
    // 清除按钮
    clear () {
      this.tableForm = {
        keyword: null
      }
    },
    // 删除
    del (row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms(row)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange (val) {
      this.tableForm.pagesize = val
      this.tableForm.page = 1
      this.getList()
    },
    handleCurrentChange (val) {
      this.tableForm.page = val
      this.getList()
    },
    // 打开弹窗
    openWindow (item) {
      this.questionInfo = item
      this.$nextTick(() => {
        this.$refs.questionWindow.openWindow()
      })
    },
    // type (questionType) {
    //   console.log(questionType)
    // },
    // 渲染列表
    async getList () {
      const { data: { counts, items } } = await randoms(this.tableForm)
      // console.log(counts, items)
      this.tableData = items
      this.total = counts
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
a{
    color: blue;
}
</style>
